'use strict';

const conn = require('../../libs/Conn');
const _ = require('underscore');

module.exports = async function (params) {
    let progress_type;
    let progress_query_type;
    if(params.progress_type === '11' || params.progress_type === '12' || params.progress_type === '13'){
        progress_type = 'structural_solid_concrete_strength';
    }else if(params.progress_type === '14' || params.progress_type === '15' || params.progress_type === '16'){
        progress_type = 'reinforcement_data';
    }else{
        progress_query_type = params.progress_type;
        switch (params.progress_type) {
            case '1':
                progress_type = 'foundation_test_tanks';
                break;
            case '2':
                progress_type = 'material_check_a';
                break;
            case '3':
                progress_type = 'foundation_test';
                break;
            case '4':
                progress_type = 'foundation_inspection';
                break;
            case '5':
                progress_type = 'foundation_based_division';
                break;
            case '6':
                progress_type = 'main_division';
                break;
            case '7':
                progress_type = 'first_conversion_reinforcement';
                break;
            case '8':
                progress_type = 'material_check_b';
                break;
            case '9':
                progress_type = 'standard_reinforcement';
                break;
            case '10':
                progress_type = 'top_reinforcement';
                break;
            case '17':
                progress_type = 'masonry_mortar_strength';
                break;
            case '18':
                progress_type = 'planting_anchor';
                break;
            case '19':
                progress_type = 'concrete_structure';
                break;
            case '20':
                progress_type = 'material_check_c';
                break;
            case '21':
                progress_type = 'main_structure';
                break;
            case '22':
                progress_type = 'other_projects';
                break;
        }
    }
    let progressName = progress_type;
    let updateObj = {};
    let updateSql = ``;
    if(progress_query_type){
        updateSql = `UPDATE engineering_progress_information_data SET  progress_status = 3 WHERE engineering_id = '${params.engineering_id}' AND progress_type = ${progress_query_type}`
    }
    updateObj[progressName] = 3;
    let sql = `UPDATE engineering_progress_file SET ? WHERE engineering_id = ?;${updateSql}`;
    await conn(sql,[updateObj,params.engineering_id]);

    if(params.progress_type === '4' || params.progress_type === '5' || params.progress_type === '6'){
        let rec = await conn (`SELECT foundation_inspection,foundation_based_division,main_division FROM engineering_progress_file WHERE engineering_id = '${params.engineering_id}'`);
        let num = 0;
        for(let data in rec[0]){
            if(rec[0][data]!==3){
                num++;
                break;
            }
        }
        if(num ===0 ){
            await conn(`Update engineering_progress_file SET important_part_acceptance = 3 WHERE engineering_id = '${params.engineering_id}';UPDATE engineering_progress_information_data SET  progress_status = 3 WHERE engineering_id = '${params.engineering_id}' AND progress_type in (4,5,6)`)
        }
    }else if(params.progress_type === '11' || params.progress_type === '12' || params.progress_type === '13'|| params.progress_type === '14'|| params.progress_type === '15'|| params.progress_type === '16'|| params.progress_type === '17'|| params.progress_type === '18'){
        let rec = await conn (`SELECT structural_solid_concrete_strength,reinforcement_data,masonry_mortar_strength,planting_anchor FROM engineering_progress_file WHERE engineering_id = '${params.engineering_id}'`);
        let num = 0;
        for(let data in rec[0]){
            if(rec[0][data]!==3){
                num++;
                break;
            }
        }
        if(num ===0 ){
            await conn(`Update engineering_progress_file SET quality_sampling = 3 WHERE engineering_id = '${params.engineering_id}';UPDATE engineering_progress_information_data SET  progress_status = 3 WHERE engineering_id = '${params.engineering_id}' AND progress_type in (11,12,13,14,15,16,17,18)`)
        }
    }
    let rec = await conn(`SELECT
                            foundation_test_tanks,
                            material_check_a,
                            foundation_test,
                            important_part_acceptance,
                            first_conversion_reinforcement,
                            material_check_b,
                            standard_reinforcement,
                            top_reinforcement,
                            quality_sampling,
                            concrete_structure,
                            material_check_c,
                            main_structure,
                            other_projects
                        FROM
                            engineering_progress_file
                        WHERE
	                        engineering_id = '${params.engineering_id}'`);
            let num = 0;
            for(let data in rec[0]) {
                if (rec[0][data] !== 3) {
                    num++;
                    break;
                }
            }
            if(num === 0){
                let sql = `UPDATE engineering_file SET status = 2 WHERE engineering_id = '${params.engineering_id}'`;
                await conn(sql);
            }

    return {success: true};

};